package com.avexVertex.controller;

import com.avexVertex.pojo.Dept;
import com.avexVertex.pojo.Result;
import com.avexVertex.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询全部的部门
     * @return
     */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
//        System.out.println("查询全部的部门数据");
        log.info("查询全部的部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id){
//        System.out.println("根据id删除部门，id="+id);
        log.info("根据id删除部门，id={}", id);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Dept dept){
//        System.out.println("新增部门,dept= "+dept);
        log.info("新增部门,dept= {}", dept);
        deptService.add(dept);
        return Result.success();
    }


    /**
     * 根据id查询部门
     * @param deptId
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer deptId){
//        System.out.println("根据Id查询部门，ID: "+deptId);
        log.info("根据Id查询部门，ID: {}", deptId);
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);
    }

    /**
     * 修改部门
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门: "+dept);
        log.info("修改部门: {}", dept);
        deptService.update(dept);
        return Result.success();
    }

}
